﻿using FreeSql;
using ManaMaple.Auth.Interface;
using ManaMaple.Entity.Sys;
using ManaMaple.IRepository.Sys;
using ManaMaple.Models.User.Request;
using ManaMaple.Repository.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManaMaple.Repository.Sys
{
    /// <summary>
    /// 用户信息仓储
    /// </summary>
    public class SysUsersRepository : ShareRepository<sys_user>, ISysUsersRepository
    {
        public SysUsersRepository(IFreeSql fsql, UnitOfWorkManager unitOfWork, IAuth auth) : base(fsql, unitOfWork, auth)
        {
        }
        /// <summary>
        /// 修改用户最后登陆时间
        /// </summary>
        /// <param name="sys_Users"></param>
        /// <returns></returns>
        public async Task<bool> UpdUserLasLoginTime(int userId)
        {
            var i = await this.UpdateDiy.Where(t=>t.Id == userId).Set(t => new { LastLoginTime = DateTime.Now, UptDtt = DateTime.Now }).ExecuteAffrowsAsync();
            //var i = await this.Orm.Update<sys_user>().Set(t => t.LastLoginTime == DateTime.Now).ExecuteAffrowsAsync();
            return i > 0;
        }


        #region 用户管理  

        /// <summary>
        /// 用户禁用或启用
        /// </summary>
        /// <returns></returns>
        public async Task<bool> EnableUser(OperateUserRequest dto)
        {
            var i = await this.UpdateDiy.Where(t => t.Id == dto.Id).Set(t => new { IsEnable = dto.IsEnable, Uptr = CurrenUserInfo.UserID, UptrName = CurrenUserInfo.UserName, UptDtt = DateTime.Now }).ExecuteAffrowsAsync();
            return i > 0;
        }
        #endregion
    }
}
